Schema stitching
Schema stitching とは、2 つ以上の異なる GraphQL スキーマを1つに統合することを指す。この統合方法にはいくつかのパターンが考えられる。これをサポートする機能は、JavaScript/TypeScript を利用している場合には Apollo 製の graphql-tools に含まれている。Schema Stitching を考える上で、主に利用すると思われる graphql-tools の機能には以下がある。
GraphQLSchema について
executable/non-executavle
api
server で実行
local/remote
local
remote (introspection, link)
extend remote (delegate)
transform
merge
schema stitching
executable & executable
executable & non-executable
conflict
schema federation
ライブラリ
graphql modules
graphql binding
gramps
まだわからないこと
Apollo の namespace
schema federation
https://medium.com/@aaivazis/beyond-schema-delegation-e3430a942fdd
https://medium.com/@aaivazis/a-guide-to-graphql-schema-federation-part-1-995b639ac035
graphql-binding
https://www.prisma.io/blog/graphql-binding-2-0-improved-api-schema-transforms-automatic-codegen-5934cd039db1/
https://blog.apollographql.com/the-next-generation-of-schema-stitching-2716b3b259c0
https://www.apollographql.com/docs/graphql-tools/schema-stitching.html
https://qiita.com/Hiroyuki_OSAKI/items/6cdf878e134ef879eb81
概要
仕組み
GraphQLSchema
GraphQL.js をベースに考える。GraphQL スキーマは、GraphQLSchema クラスのインスタンスと捉えることができる。
code:js
const { makeExecutableSchema } = require('graphql-tools')
const { grpahql } = require('grpahql')
const typeDefs = `
type Query {
item(id: ID!): Item
}
type Item {
id: ID!
name: String
description: String
}`
const resolvers = {
Query: {
item: (root, args, context, info) => {
return ...
}
},
}
// ここで、GraphQLSchema インスタンスが作られる
// SDL 内のフィールドとリゾルバ定義をマッピングする
const schema = makeExecutableSchema({
typeDef,
resolvers
})
// クエリの実行
const query = `
query {
item(id: "1") {
name
description
}
}`
graphql(schema, query)
.then(result => console.log(result))
リモートスキーマ
makeRemoteExecutableSchema を利用すると、外部の GraphQL API エンドポイントから GraphQLSchema インスタンスを作成できる。これは、スキーマ定義と GraphQL API エンドポイントの2つを引数にとる。